var AsyncLock = require('async-lock');

const {
    watchVideo, //用户看视频
} = require('../lib/action');

const {
    Msg,
    Code,
    LockKey
} = require('../lib/consts');

module.exports = function (app) {
    return new Handler(app);
};
const Handler = function (app) {
    this.app = app;
};

const handler = Handler.prototype;

var lock = new AsyncLock({timeout: 1000});

/**
 * 用户观看视频
 * @param msg
 * @param session
 * @param next
 * @returns {Promise<void>}
 */
handler.c2sWatchVideo = async function (msg, session, next) {
    let self = this;
    let uid = session.get('uid');
    //获取任务锁
    lock.acquire(LockKey.VIDEO_LOCK + uid, async function () {
        try {
            //观看视频
            await watchVideo.call(self, {session, uid});
            next(null, {code: Code.SUC});
        } catch (err) {
            console.error('c2sWatchVideo err 1:' + err);
            next(null, {code: Code.ERR, msg: err.toString()});
        }
    }, function (err, ret) {
        //锁释放
        if (!!err) {
            console.error('c2sWatchVideo err 2:' + err)
        }
    })
}



